import pygame, math, sys
pygame.init()

def dist(pt1, pt2):
    x1 = pt1[0]
    x2 = pt2[0]
    y1 = pt1[1]
    y2 = pt2[1]
    return math.sqrt(((x2-x1)**2)+((y2-y1)**2))

screenWidth = 800
screenHeight = 600

screenSize = screenWidth, screenHeight
screen = pygame.display.set_mode(screenSize)

ballSurf1 = pygame.image.load("ball1.png")
ballRect1 = ballSurf1.get_rect()
ballRadius1 = ballRect1.width/2

speedx1 = 2
speedy1 = 2
speed1 = [speedx1, speedy1]

ballSurf2 = pygame.image.load("ball2.png")
ballRect2 = ballSurf2.get_rect()
ballRadius2 = ballRect2.width/2

speedx2 = 3
speedy2 = 1
speed2 = [speedx2, speedy2]

ballSurf3 = pygame.image.load("ball3.png")
ballRect3 = ballSurf3.get_rect()
ballRadius3 = ballRect3.width/2

speedx3 = 1
speedy3 = 3
speed3 = [speedx3, speedy3]

ballSurf4 = pygame.image.load("ball4.png")
ballRect4 = ballSurf4.get_rect()
ballRadius4 = ballRect4.width/2

speedx4 = 4
speedy4 = 3
speed4 = [speedx4, speedy4]


ballRect2 = ballRect2.move(500,200)

ballRect3 = ballRect3.move(200,500)

ballRect4 = ballRect4.move(350,350)

red = 0
green = 0
blue = 0
bgColor = red, green, blue

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()
            
    ballRect1 = ballRect1.move(speed1)
    if (ballRect1.left < 0 or ballRect1.right > screenWidth):
        speed1[0] = speed1[0]*-1
    if (ballRect1.top < 0 or ballRect1.bottom > screenHeight):
        speed1[1] = speed1[1]*-1
        
    ballRect2 = ballRect2.move(speed2)
    if (ballRect2.left < 0 or ballRect2.right > screenWidth):
        speed2[0] = speed2[0]*-1
    if (ballRect2.top < 0 or ballRect2.bottom > screenHeight):
        speed2[1] = speed2[1]*-1
        
    ballRect3 = ballRect3.move(speed3)
    if (ballRect3.left < 0 or ballRect3.right > screenWidth):
        speed3[0] = speed3[0]*-1
    if (ballRect3.top < 0 or ballRect3.bottom > screenHeight):
        speed3[1] = speed3[1]*-1
        
    ballRect4 = ballRect4.move(speed4)
    if (ballRect4.left < 0 or ballRect4.right > screenWidth):
        speed4[0] = speed4[0]*-1
    if (ballRect4.top < 0 or ballRect4.bottom > screenHeight):
        speed4[1] = speed4[1]*-1
        
    if (ballRect1.right > ballRect2.left
        and ballRect1.left < ballRect2.right):
        if (ballRect1.bottom > ballRect2.top and
            ballRect1.top < ballRect2.bottom):
            if (dist(ballRect1.center, ballRect2.center)
                < ballRadius1 + ballRadius2): 
                if red <235:
                    red+=20
                else:
                    red = 0
                if green <255:
                    green+=1
                if blue <255:
                    blue+=1
                else:
                    blue = 0
                speed1[0] = speed1[0] * -1
                speed1[1] = speed1[1] * -1
                speed2[0] = speed2[0] * -1
                speed2[1] = speed2[1] * -1
                
    if (ballRect1.right > ballRect3.left
        and ballRect1.left < ballRect3.right):
        if (ballRect1.bottom > ballRect3.top and
            ballRect1.top < ballRect3.bottom):
            if (dist(ballRect1.center, ballRect3.center)
                < ballRadius1 + ballRadius3):  
                if red <255:
                    red+=1
                else:
                    red = 0
                if green <235:
                    green+=20
                else:
                    green = 0
                if blue <255:
                    blue+=1
                else:
                    blue = 0
                speed1[0] = speed1[0] * -1
                speed1[1] = speed1[1] * -1
                speed3[0] = speed3[0] * -1
                speed3[1] = speed3[1] * -1
             
    if (ballRect3.right > ballRect2.left
        and ballRect3.left < ballRect2.right):
        if (ballRect3.bottom > ballRect2.top and
            ballRect3.top < ballRect2.bottom):
            if (dist(ballRect3.center, ballRect2.center)
                < ballRadius3 + ballRadius2): 
                if red <255:
                    red+=1
                else:
                    red = 0
                if green <255:
                    green+=1
                else:
                    green = 0
                if blue <235:
                    blue+=20
                else:
                    blue = 0
                speed3[0] = speed3[0] * -1
                speed3[1] = speed3[1] * -1
                speed2[0] = speed2[0] * -1
                speed2[1] = speed2[1] * -1
                
    if (ballRect1.right > ballRect4.left
        and ballRect1.left < ballRect4.right):
        if (ballRect1.bottom > ballRect4.top and
            ballRect1.top < ballRect4.bottom):
            if (dist(ballRect1.center, ballRect4.center)
                < ballRadius1 + ballRadius4):
                if red <250:
                    red+=5
                else:
                    red = 0
                if green <235:
                    green+=20
                else:
                    green = 0
                if blue <255:
                    blue+=1
                else:
                    blue = 0
                speed1[0] = speed1[0] * -1
                speed1[1] = speed1[1] * -1
                speed4[0] = speed4[0] * -1
                speed4[1] = speed4[1] * -1
                
    if (ballRect4.right > ballRect2.left
        and ballRect4.left < ballRect2.right):
        if (ballRect4.bottom > ballRect2.top and
            ballRect4.top < ballRect2.bottom):
            if (dist(ballRect4.center, ballRect2.center)
                < ballRadius4 + ballRadius2):
                if red <235:
                    red+=20
                else:
                    red = 0
                if green <255:
                    green+=1
                else:
                    green = 0
                if blue <250:
                    blue+=5
                else:
                    blue = 0
                speed4[0] = speed4[0] * -1
                speed4[1] = speed4[1] * -1
                speed2[0] = speed2[0] * -1
                speed2[1] = speed2[1] * -1
                
    if (ballRect3.right > ballRect4.left
        and ballRect3.left < ballRect4.right):
        if (ballRect3.bottom > ballRect4.top and
            ballRect3.top < ballRect4.bottom):
            if (dist(ballRect3.center, ballRect4.center)
                < ballRadius3 + ballRadius4): 
                if red <255:
                    red+=1
                else:
                    red = 0
                if green <250:
                    green+=5
                else:
                    green = 0
                if blue <235:
                    blue+=20
                else:
                    blue = 0
                speed3[0] = speed3[0] * -1
                speed3[1] = speed3[1] * -1
                speed4[0] = speed4[0] * -1
                speed4[1] = speed4[1] * -1
    
    bgColor = red, green, blue
    screen.fill(bgColor)
    screen.blit(ballSurf1, ballRect1)
    screen.blit(ballSurf2, ballRect2)
    screen.blit(ballSurf3, ballRect3)
    screen.blit(ballSurf4, ballRect4)
    pygame.display.flip()